package epsofts.Neutron;

import java.util.Vector;

import org.neuroph.core.NeuralNetwork;

/**
 * @version 1.0
 * @created 14-sept.-2006 22:47:59
 */
public class EvalueurReseauNeurones implements IEvalueur
{
	public static final String	LABEL						= "(NN)";
	public static final Boolean	ENABLED						= true;
	
	private final NeuralNetwork nn;
	
	public EvalueurReseauNeurones()
	{
		// load neural network NeuralNetwork
		nn = NeuralNetwork.load("NN.nnet");
	}

	public void finalize() throws Throwable
	{
		super.finalize();
	}

	private static double[] inputize(Plateau p)
	{
		double[] inputs = new double[34];
		
		p = p.normaliser(eCase.ROUGE, true);
		short i = 0;
		
		for(eCase c : p.getCases())
		{
			inputs[i] = c == eCase.ROUGE ? 1.0 : c == eCase.BLEUE ? -1.0 : 0.0;
			++i;
		}
		
		int a = p.getNeutron();
		int y = (((int) (a / 5))-1);
		int x = a - (5*y) - 5;
		int ninput = (3*y) + (x%3);
		
		for(int j=0; j < 9; ++j)
		{
			inputs[i] = (j == ninput ? 1.0 : 0.0);
			++i;
		}
		
		return inputs;
	}
	
	/**
	 * Evalue le plateau en y attribuant un score de +100 � -100.
	 * 
	 * 
	 * @param plateau
	 */
	public float evalueurPlateau(final Plateau p)
	{
		int facteur = p.getTour() == eCase.ROUGE ? 1 : -1;
		
		// set network input
		nn.setInput( inputize(p) );
		
		// calculate network
		nn.calculate();
		
		// get network output
		return facteur * (float) nn.getOutput()[0];
	}

}
